package com.handpay.nocardpay.baselibrary.setting.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.account.view.ILogoutView;
import com.handpay.nocardpay.baselibrary.account.view.activity.LoginActivity;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.pay.unipay.view.fragment.ShowHTMLFragment;
import com.handpay.nocardpay.baselibrary.setting.presenter.SettingPresenterImpl;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.NoCardPayConstant;

import java.util.HashMap;

/**
 * Created by sxshi on 2017/8/18.
 */

public class SettingFragment extends BaseFragment implements ILogoutView {
    private SettingPresenterImpl settingPresenter;
    private TextView setting_help, setting_version_upgrade, tv_registation, tv_gesture_pwd;
    private Button bt_setting_exit;
    private View gesture_pwd_line_view;

    public static SettingFragment newInstance() {
        Bundle args = new Bundle();
        SettingFragment fragment = new SettingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_setting;
    }

    @Override
    public String getFragmentTitle() {
        return _mActivity.getString(R.string.setting);
    }

    @Override
    public void initView(View view) {
        Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true);
        setting_help = (TextView) view.findViewById(R.id.setting_help);
        setting_version_upgrade = (TextView) view.findViewById(R.id.setting_version_upgrade);
        bt_setting_exit = (Button) view.findViewById(R.id.bt_setting_exit);
        tv_registation = (TextView) view.findViewById(R.id.tv_registation);
        tv_gesture_pwd = (TextView) view.findViewById(R.id.tv_gesture_pwd);
        gesture_pwd_line_view = view.findViewById(R.id.gesture_pwd_line_view);
        if (NoCardPayConfig.isNoCardPay == NoCardPayConstant.FASTBILL || AccountUtils.getAccountStatus(_mActivity) == AccountUtils.AccountStatus.NOUPLOAD) {
            tv_gesture_pwd.setVisibility(View.GONE);
            gesture_pwd_line_view.setVisibility(View.GONE);
        } else {
            tv_gesture_pwd.setVisibility(View.VISIBLE);
            gesture_pwd_line_view.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initListener() {
        setting_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(ShowHTMLFragment.newInstance(_mActivity.getString(R.string.new_setting_help_text), NoCardPayConfig.USERINFO_URL));
            }
        });

        setting_version_upgrade.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(AboutFragment.newInstance());
            }
        });

        bt_setting_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, Object> params = new HashMap<String, Object>();
                params.put(BaseProtocolKey.PHONE, SecureManager.getInstance().des(AccountUtils.getAccountPhone(_mActivity), 1, null));
                params.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
                settingPresenter.userLogOut(params, true);
            }
        });
        tv_registation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(ShowHTMLFragment.newInstance(_mActivity.getString(R.string.new_setting_user_read_text), NoCardPayConfig.REGISTATION));
            }
        });

        tv_gesture_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(SettingGesturePWDFragment.newInstance());
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        settingPresenter = new SettingPresenterImpl(_mActivity);
        settingPresenter.setLogoutView(this);


    }

    @Override
    public void logOutSuccess() {
        Intent intent = new Intent();
        intent.setClass(_mActivity, LoginActivity.class);
        intent.putExtra(BaseProtocolKey.LOGIN_TYPE, NoCardPayConfig.isNoCardPay);
        _mActivity.startActivity(intent);
        _mActivity.finish();
    }
}
